// Copyright (c) 2012 SameGoal LLC.
// All Rights Reserved.
// Author: Andy Hochhaus <ahochhaus@samegoal.com>

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef NET_HTTP_SERVER_EVENT_PROXY_REQUEST_H_
#define NET_HTTP_SERVER_EVENT_PROXY_REQUEST_H_

#include <sys/time.h>

#include <string>

#include "base/basictypes.h"

struct bufferevent;

namespace sg {
namespace net {
namespace http {
namespace server {
namespace event {

class Request;
class EventServer;

class ProxyRequest {
 public:
  ProxyRequest(Request *req, EventServer *event_server);
  ~ProxyRequest();

  void To(const std::string &host, const uint16 port);

 private:
  Request *req_;
  EventServer *event_server_;
  bufferevent *bev_;
  timeval timeout_;

  static void ReadCallback(bufferevent *buff_ev, void *ctx);
  static void EventCallback(bufferevent *buff_ev, int16 events, void *ctx);

  static std::string ExtractValue(const std::string &str,
                                  const std::string &key);
  static bool ExtractChunkLength(const std::string &str,
                                 const uint64 offset,
                                 uint64 *chunk_prefix_bytes,
                                 uint64 *chunk_length);

  DISALLOW_COPY_AND_ASSIGN(ProxyRequest);
};

}  // namespace event
}  // namespace server
}  // namespace http
}  // namespace net
}  // namespace sg

#endif  // NET_HTTP_SERVER_EVENT_PROXY_REQUEST_H_
